﻿using DotNetUniLodz.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;

namespace DotNetUniLodz.HelperClasses
{
    public class MyRoleProvider : RoleProvider
    {
        DotNetULEntities db = new DotNetULEntities();

        #region NotImplemented

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            throw new NotImplementedException();
        }

        public override string[] GetRolesForUser(string username)
        {
            var result = from u in db.Users
                         where u.User_login == username
                         select u.Role.Role_name;

            return result.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            var result = from r in db.Roles
                         join u in db.Users on r.Role_id equals u.Role_id
                         where r.Role_name == roleName
                         select u.User_login;

            return result.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var user = db.Users.FirstOrDefault(u => u.User_login == username);

            if (user != null)
            {
                return user.Role.Role_name == roleName;
            }

            return false;
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            return db.Roles.Any(r => r.Role_name == roleName);
        }

        #endregion
    }
}